package servlet.hou;

import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


//宿管增加公共设施
@WebServlet("/hou/addPublicByHou")
public class AddPublicByHouServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String publicName= request.getParameter("publicName");
       Integer publicCount= Integer.valueOf(request.getParameter("publicCount"));
        String user_token = request.getHeader("user_token_header");
        Integer buildId = JwtUtils.getHouBuildIdFromToken(user_token,"buildId");

        Integer data=UserDaoImpl.addPublicByHou(publicName,publicCount,buildId);
        Result result = new Result();
        if(data!=null){
            result.success(data);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }


    }
}
